package com.xianyu.us.common.config;

import java.util.LinkedHashMap;

import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.cache.MemoryConstrainedCacheManager;
import org.apache.shiro.mgt.RememberMeManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.CookieRememberMeManager;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;

@Configuration
public class SpringShiroConfig {
	/**
	 * 配置Session会话对象
	 * @return
	 */
		@Bean
		public SessionManager sessionManager() {
			DefaultWebSessionManager sessionManager=new DefaultWebSessionManager();
			sessionManager.setGlobalSessionTimeout(60*60*1000);
			return sessionManager;
		}
	/**
	 * 添加记住我配置
	 * @return
	 */
	@Bean
	public RememberMeManager rememberMeManager() {
		CookieRememberMeManager rememberMeManager = new CookieRememberMeManager();
		SimpleCookie cookie =new SimpleCookie("rememberMe");
		cookie.setMaxAge(7*24*3600);
		rememberMeManager.setCookie(cookie);
		return rememberMeManager;
	}
	/**
	 * 配置授权cache
	 */
	@Bean
	public CacheManager shiroCacheManager() {
		return  new MemoryConstrainedCacheManager();
	}


	/**
	 * 在Shiro配置类中添加SecurityManager配置
	 * @return
	 */
	@Bean
	public SecurityManager securityManager(Realm realm,CacheManager cacheManager,
			RememberMeManager rememberMeManager	) {
		DefaultWebSecurityManager securityManager=
				new DefaultWebSecurityManager();
		securityManager.setRealm(realm);
		securityManager.setCacheManager(cacheManager);
		securityManager.setRememberMeManager(rememberMeManager);
		return securityManager;
	}
	/**
	 * 在Shiro配置类中添加ShiroFilterFactoryBean对象的配置,过滤器
	 * @return
	 */
	@Bean
	public  ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
			 //构建对象
			 ShiroFilterFactoryBean fBean=new ShiroFilterFactoryBean();
			 //设置安全认证授权对象
			 fBean.setSecurityManager(securityManager);
			 
			 LinkedHashMap<String,String> map= new LinkedHashMap<>();
			 
			 map.put("/css/**","anon");
			 map.put("/fonts/**","anon");
			 map.put("/images/**","anon");
			 map.put("/js/**","anon");
			 map.put("/assets/**","anon");
			 map.put("/user/**", "anon");//点击登陆提交地址
			 map.put("/index", "anon");//放行主页
			 map.put("/register", "anon");//手机注册
			 map.put("/register2", "anon");//邮箱注册
			 map.put("/pages/**", "anon");//
			 map.put("/goods/**", "anon");//
			 map.put("/Shop/**", "anon");//
			 map.put("/goods", "anon");//
			 map.put("/doPageUI", "anon");//放行分页获取请求
			 map.put("/order/notify", "anon");//支付宝异步通知，不能拦截
			 map.put("/", "anon");//放行默认页
			 map.put("/doLogout", "logout");//注销
			 //除了匿名访问的资源,其它都要认证("authc")后访问
			 //当选择了记住我功能，认证方式进行修改
			 map.put("/**","user");
			//设置登陆页面
			 fBean.setLoginUrl("/login");
			 //设置过滤规则
			 fBean.setFilterChainDefinitionMap(map);
			 return fBean;
		 
	}
	
	/**
	 * 配合thymeleaf使用
	 * @return
	 */
	@Bean
	public ShiroDialect getShiroDialect() {
		return new ShiroDialect();
	}
}
